<?php

//All the file calls required for the definitions of the class.
require_once WP_PLUGIN_DIR . '/diapucp-events/defines.php' ;
require_once DPEM_LIB_PATH . '/smartmetabox/smartmetabox.php';

class dia_pucp_events_init {
    private $metaboxes = array();

/**
 *
 * Initialize the "Events" framework.
 *
 * Constructor method. Attaches to the respective hooks the methods to declare
 * the events post-type, hide the extra field that shows all the metadata to fill
 * and create the respective boxes to use this post-type.
 *
 * @param no parameters.
 * @return no returns.
*/
    public function dia_pucp_events_init(){
        //add_action( 'init', array( &$this, 'create_events' ) );
        //add_action( 'admin_head', array( &$this, 'remove_meta_box_base' ) );

        $this->create_meta_fields();
    }


/**
 *
 * Create the Event's post-type
 *
 * Method that registers the Event's posr-type and declares all the options the
 * post-type will have.
 *
 * @param no parameters.
 * @return no returns.
*/
    public function create_events() {
      $labels = array(
        'name' => _x('Events', 'post type general name'),
        'singular_name' => _x('Events', 'post type singular name'),
        'add_new' => _x('Add New', 'Events'),
        'add_new_item' => __('Add New Event'),
        'edit_item' => __('Edit Event'),
        'new_item' => __('New Event'),
        'view_item' => __('View Event'),
        'search_items' => __('Search Events'),
        'not_found' =>  __('No Events found'),
        'not_found_in_trash' => __('No Events found in Trash'),
        'parent_item_colon' => '',
      );

      $supports = array('title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields', 'genesis-seo', 'genesis-layout');

      $taxonomies = array('category', 'post_tag');

      register_post_type( 'event',
        array(
          'labels' => $labels,
          'public' => true,
          'supports' => $supports,
          'taxonomies' => $taxonomies,
          'menu_icon' => DPEM_IMAGES_URL . '/calendar-16.png',
        ) );

      flush_rewrite_rules();
    }

/**
 *
 * Declare all the custom fields for events.
 *
 * Method that creates all the custom fields the vents will use, calling the API
 * to create a custom field according to certain specifications. Form more information,
 * check the smarmetabox.php file.
 *
 * @param no parameters.
 * @return no returns.
*/
    public function create_meta_fields(){
        $this->metaboxes[] = add_smart_meta_box('eventschedule', array(
            'title'     => __('Event\'s Information'),
            'pages'     => array('post'),
            'context'   => 'normal',
            'priority'  => 'high',
            'validate'  => TRUE,           //Flag: This metabox must be validated before sending the info.
            'fields'    => array(
                array(
                    'name' => __( 'Turn the post into an event?: ' ),
                    'id' => DPEM . 'is_event',
                    'desc' => __( '' ),
                    'default' => __( 'false' ),
                    'type' => 'checkbox',
                    'required' => FALSE,   //Flag: The field must have a value
                ),
                array( 'type' => 'changeline' ),
                array(
                    'name' => __( 'Where: ' ),
                    'id' => DPEM . 'localization',
                    'desc' => __( '' ),
                    'default' => __( 'To be announced' ),
                    'type' => 'text',
                    'required' => FALSE,   //Flag: The field must have a value
                ),
                array( 'type' => 'changeline' ),
                array(
                    'name' => __( 'Organized by: ' ),
                    'id' => DPEM . 'organizer',
                    'desc' => __( '' ),
                    'default' => __( '' ),
                    'type' => 'text',
                    'required' => FALSE,   //Flag: The field must have a value
                ),
                array( 'type' => 'changeline' ),
                array(
                    'name' => __( 'Location\'s Description' ),
                    'id' => DPEM . 'loc_desc',
                    'desc' => __( '' ),
                    'default' => __( '' ),
                    'type' => 'textarea',
                    'required' => FALSE,   //Flag: The field must have a value
                ),
                array( 'type' => 'changeline' ),
                array(
                    'name' => __( 'Start Date: ' ),
                    'id' => DPEM . 'startdate',
                    'type' => 'datepicker',
                    'default' => date('m/d/Y'), //Recommended if you are going to make the field required.
                    'required' => TRUE,         //Flag: The field must have a value
                    'compare' => 1              //Flag: The order of the object.
                ),
                array(
                    'name' => __( 'Start Time: ' ),
                    'id' => DPEM . 'starttime',
                    'type' => 'timepicker',
                    'compare' => 1
                ),
                array( 'type' => 'changeline' ),
                array(
                    'name' => __( 'End Date: ' ),
                    'id' => DPEM . 'enddate',
                    'type' => 'datepicker',
                    'default' => date('m/d/Y'),
                    'required' => TRUE,
                    'compare' => 2
                ),
                array(
                    'name' => __( 'End Time: '),
                    'id' => DPEM . 'endtime',
                    'type' => 'timepicker',
                    'compare' => 2
                ),
                //array( 'type' => 'changeline' ),
                array(
                    'name' => __( '' ),
                    'id' => DPEM . 'metabox_message',
                    'desc' => __('Fields with ** are required'),
                    'type' => 'label'
                ),
            )
        ));
    }

/**
 *
 * Hide the wordpress field that controls the metadata.
 *
 * Method that hides the metadata field that comes with wordpress. The metadata
 * of this post-type is managed through the custom fields created above.
 *
 * @param no parameters.
 * @return no returns.
*/
    public function remove_meta_box_base(){
         remove_meta_box( 'postcustom' , 'event' , 'normal');
    }

}

//And... instance the class.
new dia_pucp_events_init();

?>
